package ifmo.compiler.task21.lexer.tokens.symbols;

import ifmo.compiler.task21.lexer.tokens.TokenType;

import java.util.Map;
import java.util.HashMap;

/**
 * Factory to create symbols
 *
 * @author byaminov
 */
public class SymbolFactory {

    private static Map<String, SymbolToken> value2symbol = new HashMap<String, SymbolToken>();
    private static Map<TokenType, SymbolToken> type2symbol = new HashMap<TokenType, SymbolToken>();
    static {
        put(new BracketLeft());
        put(new BracketRight());
        put(new BracketCentipoiseLeft());
        put(new BracketCentipoiseRight());
        put(new BracketSquareLeft());
        put(new BracketSquareRight());
        put(new Colon());
        put(new Semicolon());
        put(new Comma());
        put(new Assign());
        put(new Not());
        put(new Dot());
        put(new Equal());
        put(new NotEqual());
        put(new Le());
        put(new Lt());
        put(new Ge());
        put(new Gt());
        put(new Mult());
        put(new Divide());
        put(new Plus());
        put(new Minus());
    }

    public static SymbolToken createToken(String value) {
        SymbolToken s = value2symbol.get(value);
        if (s == null) {
            throw new IllegalArgumentException("Unknown symbol: " + value);
        }
        return s;
    }

    public static SymbolToken createToken(TokenType type) {
        SymbolToken s = type2symbol.get(type);
        if (s == null) {
            throw new IllegalArgumentException("Not a symbol type: " + type);
        }
        return s;
    }

    private static void put(SymbolToken i) {
        value2symbol.put(i.getValue(), i);
        type2symbol.put(i.getType(), i);
    }
}
